package translatorplugin.popup.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

import translatorplugin.Activator;
import translatorplugin.service.Translator;
import translatorplugin.service.language.Language;

public class Translate implements IObjectActionDelegate {

	private static final String AUTENTIA_TRANSLATOR = "AutentiaTranslator";
	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public Translate() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {

		SelectedText selectedText;
		String translatedText;

		IEditorPart editor = getActiveEditor();
		if (editor instanceof AbstractTextEditor) {
			selectedText = getSelectedText(editor);

			if (selectedText.getSelectedText().equals("")) {
				MessageDialog.openError(shell, AUTENTIA_TRANSLATOR, "There is no selected text.");
				return;
			}

			translatedText = getTranslateText(getSourceLanguage(action),
					getDestLanguage(action), selectedText.getSelectedText());

			if (action.getId().contains("window")){
				MessageDialog.openInformation(shell, AUTENTIA_TRANSLATOR, translatedText);
				return;
			} 
			if (action.getId().contains("replace")) {
				replaceText(editor, selectedText, translatedText);
				return;
			}
		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {

	}

	private IEditorPart getActiveEditor() {
		return Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getActiveEditor();
	}

	private SelectedText getSelectedText(IEditorPart editor) {
		SelectedText selectedText;

		try {
			final ISelection selection = editor.getEditorSite().getSelectionProvider().getSelection();
			final ITextSelection textSelection = (ITextSelection) selection;
			selectedText = new SelectedText(textSelection.getText(), textSelection.getOffset(), textSelection.getLength());
		} catch (Exception e) {
			selectedText = new SelectedText("", 0, 0);
		}
		return selectedText;
	}

	private void replaceText(IEditorPart editor, SelectedText selectedText, String translatedText) {
		final ITextEditor textEditor = (ITextEditor) editor;
        final IDocumentProvider dp = textEditor.getDocumentProvider();
        final IDocument doc = dp.getDocument(editor.getEditorInput());
        
        try {
        	
			doc.replace(selectedText.offset, selectedText.getLength(), translatedText);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
        
	}
	
	private String getTranslateText(Language sourceLanguage,
			Language destLanguage, String selectedText) {
		return Translator.translate(sourceLanguage, destLanguage, selectedText);
	}

	private Language getSourceLanguage(IAction action) {
		if (action.getId().contains("EnglishToSpanish")) {
			return Language.English;
		}
		return Language.Spanish;
	}

	private Language getDestLanguage(IAction action) {
		if (action.getId().contains("EnglishToSpanish")) {
			return Language.Spanish;
		}
		return Language.English;
	}

}
